﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Windows
{
    public partial class FormProcedure : FormBase
    {
        private readonly IProcedurePersist _procedurePersist = null;
        public FormProcedure()
        {
            _procedurePersist = InstanceFactory.GetProcedurePersist();
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            var procedurePersist=  InstanceFactory.GetProcedurePersist();
            var list = procedurePersist.GetProcedureModels();
            this.treeListMain.DataSource = list;
            this.treeListMain.ExpandAll();
        }

        public override string HeaderText
        {
            get
            {
                return "  工序代码管理  ";
            }
        }

        private void CmdRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void CmdNewChild_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.treeListMain.FocusedNode != null)
                {
                    var parentProcedureModel = (TProcedureModel)this.treeListMain.GetDataRecordByNode(this.treeListMain.FocusedNode);
                    var form = new FormProcedureEdit(parentProcedureModel);
                    form.ShowDialog(this);
                    if (form.DialogResult == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
            }
            catch (Exception exception)
            {

                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdNewRoot_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new FormProcedureEdit(null);
                form.ShowDialog(this);
                if (form.DialogResult == DialogResult.OK)
                {
                    LoadData();
                }
            }
            catch (Exception exception)
            {

                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdDeleteNode_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.treeListMain.FocusedNode != null)
                {
                    var procedureModel = (TProcedureModel)this.treeListMain.GetDataRecordByNode(this.treeListMain.FocusedNode);
                    if (procedureModel != null &&
                        Message.MsgBox.ShowYesOrNo(String.Format("确实要删除选中的工序{0}吗？",procedureModel.ProcedureNo)))
                    {
                        _procedurePersist.DeleteChildProcedure(procedureModel.ProcedureNo);
                        LoadData();
                    }
                }
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
      
        }

        private void CmdUpdate_Click(object sender, EventArgs e)
        {
            if (this.treeListMain.FocusedNode != null)
            {
                var procedureModel = (TProcedureModel)this.treeListMain.GetDataRecordByNode(this.treeListMain.FocusedNode);
                var form = new FormProcedureUpdate(procedureModel);
                form.ShowDialog(this);
                if(form.DialogResult == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }
    }
}